<?php
/**
 * Created by PhpStorm.
 * User:吴鑫
 */

namespace backend\service;

use api\service\OrderService;
use common\models\Order;
use common\models\UserOrderCount;
use common\service\ConstatService;
use yii\db\Query ;
use Yii ;

class TaskService
{

    /**
     * 查找所有门店 放入缓存中
     * @return array|mixed
     */
    public static function countException(){
        $query = new Query();

        $countBeginTime = date("Y-m-d 00:00:00",strtotime("-1 day",strtotime('now'))) ;
        $countEndTime = date("Y-m-d 00:00:00") ;

        $query->select('*')
                ->from(UserOrderCount::tableName())
                ->where([">","exception",0])
                ->andWhere([">=","count_date",$countBeginTime])
                ->andWhere(["<","count_date",$countEndTime])
                ->orderBy("id asc") ;

        $userOrderCounts = $query->all() ;

        $updNum = 0 ;

        foreach($userOrderCounts as $key=>$val){
            $date = $val['count_date'] ;
            $beginTime = $date." 00:00:00" ;
            $endTime = date("Y-m-d H:i:s",strtotime("+1 day",strtotime($beginTime))) ;
            $sendUserId = $val['user_id'] ;

            $orderQuery = new Query();

            $orderQuery->from(Order::tableName())
                ->where(["sender_user_id"=>$sendUserId])
                ->andwhere(["status"=>ConstatService::ORDER_EXCEPTION])
                ->andwhere([">=","opt_time",$beginTime])
                ->andWhere(["<","opt_time",$endTime]) ;

            $exceptionCount = $orderQuery->count("id") ;

            $rs = UserOrderCount::updateAll(["exception"=>$exceptionCount],["id"=>$val['id']]) ;
            if($rs==1){
                $updNum = $updNum + 1;
            }
        }
        return $updNum ;
    }


}